package com.heima.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.apis.user.IUserClient;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.common.constants.UserConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import com.heima.user.service.AuthService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author menyongzhi
 * @since 2021年12月02日 15:58
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private IUserClient userClient;

    @Autowired
    private ApUserRealnameService apUserRealnameService;

    @Autowired
    private ApUserService apUserService;

    @Autowired
    private IWemediaClient wmUserClient;

    @Override
    public ResponseResult findList(AuthDto dto) {
        return userClient.findList(dto);
    }

    @Override
    public ResponseResult authFail(AuthDto dto) {

        LambdaQueryWrapper<ApUserRealname> apUserRealnameQueryWrapper = new LambdaQueryWrapper<>();
        apUserRealnameQueryWrapper.eq(ApUserRealname::getUserId,dto.getId());
        apUserRealnameQueryWrapper.orderByDesc(ApUserRealname::getUpdatedTime);

        ApUserRealname apUserRealnameInDb = apUserRealnameService.getOne(apUserRealnameQueryWrapper);
        if (apUserRealnameInDb!=null){
            ApUserRealname apUserRealname = new ApUserRealname();
            BeanUtils.copyProperties(apUserRealnameInDb,apUserRealname);
            apUserRealname.setStatus(UserConstants.FAIL_AUTH);
            apUserRealname.setUpdatedTime(new Date());
            apUserRealnameService.updateById(apUserRealname);
        }

        //更改ap_user中用户的状态
        ApUser apUserInDb = apUserService.getById(dto.getId());
        if (apUserInDb!=null){
            ApUser apUser= new ApUser();
            BeanUtils.copyProperties(apUserInDb,apUser);
            apUser.setStatus(1);

            apUserService.updateById(apUser);
        }

        //更改wm_user中对应用户的状态
        WmUser wmUserInDb = wmUserClient.getWmUser(dto.getId());
        if (wmUserInDb!=null){
            WmUser wmUser = new WmUser();
            BeanUtils.copyProperties(wmUserInDb,wmUser);
            wmUser.setStatus(0);

            wmUserClient.update(wmUser);
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {

        LambdaQueryWrapper<ApUserRealname> apUserRealnameQueryWrapper = new LambdaQueryWrapper<>();
        apUserRealnameQueryWrapper.eq(ApUserRealname::getUserId,dto.getId());
        apUserRealnameQueryWrapper.orderByDesc(ApUserRealname::getUpdatedTime);

        ApUserRealname apUserRealnameInDb = apUserRealnameService.getOne(apUserRealnameQueryWrapper);
        if (apUserRealnameInDb!=null){
            ApUserRealname apUserRealname = new ApUserRealname();
            BeanUtils.copyProperties(apUserRealnameInDb,apUserRealname);
            apUserRealname.setStatus(UserConstants.PASS_AUTH);
            apUserRealname.setUpdatedTime(new Date());
            apUserRealnameService.updateById(apUserRealname);
        }

        //更改ap_user中对应用户的状态
        ApUser apUserInDb = apUserService.getById(dto.getId());
        ApUser apUser= new ApUser();
        if (apUserInDb!=null){
            BeanUtils.copyProperties(apUserInDb,apUser);
            apUser.setStatus(1);
            apUser.setFlag(1);

            apUserService.updateById(apUser);
        }
        //更改wm_user中对应用户的状态
        WmUser wmUserInDb = wmUserClient.getWmUser(dto.getId());
        if (wmUserInDb==null){
            WmUser wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setImage(apUser.getImage());
            wmUser.setPhone(apUser.getPhone());
            wmUser.setCreatedTime(new Date());

            wmUserClient.save(wmUser);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
